package name.jautzy.esi.server;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import name.jautzy.esi.client.model.EsiComment;
import name.jautzy.esi.client.model.EsiUser;
import name.jautzy.esi.client.model.LoginInfo;
import name.jautzy.esi.client.service.LoginService;
import name.jautzy.esi.server.model.FitComment;

import com.extjs.gxt.ui.client.data.BaseListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class LoginServiceImpl extends RemoteServiceServlet implements
LoginService {
	private EdbDAO dao = new EdbDAO();
	
	public LoginInfo login(String requestUri) {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		LoginInfo loginInfo = new LoginInfo();
		if (user != null) {
			loginInfo.setLoggedIn(true);
			loginInfo.setAdmin(userService.isUserAdmin());
			loginInfo.setEmailAddress(user.getEmail());
			loginInfo.setNickname(user.getNickname());
			loginInfo.setLogoutUrl(userService.createLogoutURL(requestUri));
		} else {
			loginInfo.setLoggedIn(false);
			loginInfo.setLoginUrl(userService.createLoginURL(requestUri));
		}
		return loginInfo;
	}

	@Override
	public EsiUser getCurrentUser() {
		UserService userService = UserServiceFactory.getUserService();
		User usr = userService.getCurrentUser();
		EsiUser user = dao.getUser(usr.getEmail());
		if (user==null) {
			user = new EsiUser();
			user.setId(usr.getEmail());
			user.setNick("Newb");
			user.setAlliance("none");
			user.setCorpo("none");
		}
			
		return user;
	}

	@Override
	public void updateUser(EsiUser user) {
		dao.putUser(user);
	}

	@Override
	public EsiComment addComment(String fitId, String comment) {
		Date date = new Date();
		EsiUser user = getCurrentUser();
		EsiComment esiCom = new EsiComment();
		esiCom.setNick(user.getNick());
		esiCom.setAlly(user.getAlliance());
		esiCom.setCorpo(user.getCorpo());
		esiCom.setDate(date);
		esiCom.setMaker(user.getId());
		esiCom.setComment(comment);
		
		FitComment fitCom = new FitComment();
		fitCom.setMaker(user.getId());
		fitCom.setDate(date);
		fitCom.setFitId(fitId);
		fitCom.setComment(comment);
		
		dao.putComment(fitCom);
		
		return esiCom;
	}

	@Override
	public ListLoadResult<EsiComment> getComments(String fitId) {
	
		List<EsiComment> ret = new LinkedList<EsiComment>();
		
		for (FitComment com : dao.getComments(fitId))
		{
			EsiUser user = dao.getUser(com.getMaker());
			EsiComment esiCom = new EsiComment();
			esiCom.setNick(user.getNick());
			esiCom.setAlly(user.getAlliance());
			esiCom.setCorpo(user.getCorpo());
			esiCom.setMaker(user.getId());
			esiCom.setComment(com.getComment());
			esiCom.setDate(com.getDate());
			ret.add(esiCom);		
		}
		return new BaseListLoadResult<EsiComment>(ret);
	}
	

}
